<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @package  	PyroCMS
 * @subpackage  Categories
 * @category  	Module
 * @author  	PyroCMS Dev Team
 */
class Admin_Contact extends Admin_Controller {

	/**
	 * The current active section
	 * @access protected
	 * @var int
	 */
	protected $section = 'escort-personal';
	protected $_ci;
	/**
	 * The constructor
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		
		$this->_ci = &get_instance();
		$this->lang->load('be_escort');
		$this->lang->load('be_escort_contact');
	}
	
	/**
	 * Index method, lists all categories
	 * @access public
	 * @return void
	 */
	public function index($id)
	{
		$id OR redirect('admin/be_escort');
		
		$escort = $this->_ci->escort_entity->init('id', $id);
		$user = $this->_ci->user_entity->init('id', $escort->getUserId());
		$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $id);
		if(!$lockedEscort){
			$this->_ci->locked_escort_entity->createNewLockedEscortEntity($id);
			$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $id);
		}
		
		if(!$escort || !$user){
			die();
		}
		$this->data['escort'] = $escort;
		$this->data['user'] = $user;
		$this->data['lockedEscort'] = $lockedEscort;
		$this->pyrocache->delete_all('modules_m');
		$this->load->view('admin/contact/index', $this->data);
	}
	public function save(){
		$result = array();
		$result['result'] = true;
		$escortId = $this->input->post('escort_id');
		$ESCORT = $this->_ci->escort_entity->init('id',$escortId);
		$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $escortId);
		$USER = $this->_ci->user_entity->init('id', $ESCORT->getUserId());
		//$escort = new CI_Escort_entity();
		$contact_lat = $this->input->post('contact_lat', TRUE);
		$contact_lng = $this->input->post('contact_lng', TRUE);
		$contact_info_name = $this->input->post('contact_info_name', TRUE);
		$contact_info_phone = $this->input->post('contact_info_phone', TRUE);
		$contact_info_public_phone = $this->input->post('public_phone', TRUE);
		$contact_info_email = $this->input->post('contact_info_email', TRUE);
		$contact_info_public_email = $this->input->post('public_email', TRUE);
		$contact_info_address = $this->input->post('contact_info_address', TRUE);
		$contact_info_public_geolocalization = $this->input->post('public_geolocation', TRUE);
		$contact_info_show_address_on_map = $this->input->post('contact_info_show_address_on_map', TRUE);
		$contact_info_receive_message = $this->input->post('contact_info_receive_message', TRUE);
		$contact_info_public_address = $this->input->post('public_address', TRUE);
		
		if(! $this->phpvalidator->is_email($contact_info_email)){
			$result['message']= "contact_info_email_err#Email is not valid.";
			exit();
		}
		$U = clone_($this->user_entity->init('email',$contact_info_email));
		if($U AND $U->getId() != $USER->getId()){
			$result['message']= "contact_info_email_err#Email is existed in system.";
			exit();
		}
		
		$ESCORT->setEscortName($contact_info_name);
		$ESCORT->setEscortEmail($contact_info_email);
		$ESCORT->setEscortPhone($contact_info_phone);
		$ESCORT->setEscortAddress($contact_info_address);
		$ESCORT->setGeolocation($contact_lat.'#'.$contact_lng);
		$ESCORT->setPublicGeolocation($contact_info_public_geolocalization);
		$ESCORT->setPublicPhone($contact_info_public_phone);
		$ESCORT->setPublicEmail($contact_info_public_email);
		$ESCORT->setPublicAddress($contact_info_public_address);
		$ESCORT->setShowAddressOnMap($contact_info_show_address_on_map);
		$ESCORT->setReceiveMessage($contact_info_receive_message);
		$ESCORT->adminUpdate();

		$lockedEscort->setLockedPhone($this->input->post('locked_phone'));
		$lockedEscort->setLockedEmail($this->input->post('locked_email'));
		$lockedEscort->setLockedAddress($this->input->post('locked_address'));
		$lockedEscort->setLockedGeolocation($this->input->post('locked_geolocation'));
		$lockedEscort->setLockedMap($this->input->post('locked_map'));
		$lockedEscort->update();
		
		$result['message'] = lang('escort_be_escort_contact_update_success');
		echo json_encode($result);
	}
}